﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using blackhouse.WebNemo.Data;

namespace blackhouse.WebNemo.Business.Users {
	public class DaNemoUser : INemoUser {

		#region Fields

		private ImpNemoUser instance;
		private IDataAccess data;

		#endregion

		#region Properties

		public int UserId {
			get { return this.instance.UserId; }
			private set { this.instance.UserId = value; }
		}
		public string Name {
			get { return this.instance.Name; }
			private set { this.instance.Name = value; }
		}

		#endregion

		#region Constructors

		public DaNemoUser(IDataAccess dataAccess) {
			this.data = dataAccess;
			this.instance = new ImpNemoUser();
		}

		public DaNemoUser(IDataAccess dataAccess, NemoUserData data) {
			this.data = dataAccess;
			this.instance = this.ConvertToInstance(data);
			if (this.instance == null) this.instance = new ImpNemoUser();
		}

		#endregion

		#region Methods

		private ImpNemoUser ConvertToInstance(NemoUserData data) {
			ImpNemoUser user = new ImpNemoUser();
			user.UserId = data.Id;
			user.Name = data.Name;
			return user;
		}

		public INemoUser GetUser(int userId) {
			if (this.instance.Users == null) {
				NemoUserData[] nuds = this.data.GetNemoUsers();
				this.instance.Users = nuds.Select(u => new DaNemoUser(this.data, u)).ToArray();
			}
			return this.instance.GetUser(userId);
		}

		public INemoUser GetCurrentUser() {
			return this.GetUser(9999999);
		}

		public bool HasSufficientPermissions(bool redirectToAccessDenied) {
			return this.instance.HasSufficientPermissions(redirectToAccessDenied);
		}

		#endregion

	}
}